package com.heima.shop.service.impl;

import com.heima.shop.mapper.StoreDetailMapper;
import com.heima.shop.service.StoreDetailService;
import com.heima.common.exception.CustomException;
import com.heima.model.shop.dtos.StoreDetailDto;
import com.heima.model.shop.pojos.StoreDetail;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description:
 * @Version: V1.0
 */
@Service
public class StoreDetailServiceImpl extends ServiceImpl<StoreDetailMapper, StoreDetail> implements StoreDetailService {

    /**
     * 查询列表
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findPage(StoreDetailDto dto) {
        //1 参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 分页检查
        dto.checkParam();

        //2 执行查询
        // 分页
        Page page = new Page(dto.getPage(), dto.getSize());
        // 条件查询
        LambdaQueryWrapper<StoreDetail> wraaper = new LambdaQueryWrapper<>();

        // 标签列表
         if (StringUtils.isNotBlank(dto.getLabels())) {
            wraaper.like(StoreDetail::getLabels, dto.getLabels());
         }
        // 营业执照图片
         if (StringUtils.isNotBlank(dto.getBusinessLicenseUrl())) {
            wraaper.like(StoreDetail::getBusinessLicenseUrl, dto.getBusinessLicenseUrl());
         }
        // 法人代表正面图片
         if (StringUtils.isNotBlank(dto.getCorporationObverseUrl())) {
            wraaper.like(StoreDetail::getCorporationObverseUrl, dto.getCorporationObverseUrl());
         }
        // 法人代表背面图片
         if (StringUtils.isNotBlank(dto.getCorporationBackUrl())) {
            wraaper.like(StoreDetail::getCorporationBackUrl, dto.getCorporationBackUrl());
         }
        // 营业时间
         if (StringUtils.isNotBlank(dto.getBusinessHours())) {
            wraaper.like(StoreDetail::getBusinessHours, dto.getBusinessHours());
         }
        // 客服电话
         if (StringUtils.isNotBlank(dto.getBusinessPhone())) {
            wraaper.like(StoreDetail::getBusinessPhone, dto.getBusinessPhone());
         }
        // 支持救援
         if (StringUtils.isNotBlank(dto.getIsRescue())) {
            wraaper.like(StoreDetail::getIsRescue, dto.getIsRescue());
         }
        // 救援时间
         if (StringUtils.isNotBlank(dto.getRescueHours())) {
            wraaper.like(StoreDetail::getRescueHours, dto.getRescueHours());
         }
        // 救援电话
         if (StringUtils.isNotBlank(dto.getRescuePhone())) {
            wraaper.like(StoreDetail::getRescuePhone, dto.getRescuePhone());
         }
        // 公司简介
         if (StringUtils.isNotBlank(dto.getIntroduce())) {
            wraaper.like(StoreDetail::getIntroduce, dto.getIntroduce());
         }



        IPage pageResult = page(page, wraaper);

        //3 封装分页结果
        return PageResponseResult.page(dto.getPage(), dto.getSize(),
                (int) pageResult.getTotal(),pageResult.getRecords());
    }

    /**
     * 新增
     * @param storeDetail
     * @return
     */
    @Override
    public ResponseResult insert(StoreDetail storeDetail) {
        //1 参数检查
        if (storeDetail == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2 执行保存
        save(storeDetail);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(StoreDetail storeDetail) {

        //1.检查参数
        if(null == storeDetail){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2.修改
        updateById(storeDetail);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult deleteById(Long id) {
        //1.检查参数
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

}
